﻿using System.Security.Cryptography;
using System.Text;

namespace Lanbt.Core.Utility.Helper
{
    public class EncryptHelper
    {

        #region MD5加密（不可逆）

        /// <summary>
        /// md5加密后返回密文
        /// </summary>
        /// <param name="source">要加密的明文</param>
        /// <returns></returns>
        public static string MD5Encrypt(string source)
        {
            try
            {
                MD5 md5 = MD5.Create();
                byte[] data = md5.ComputeHash(Encoding.Default.GetBytes(source));
                StringBuilder sBuilder = new StringBuilder();
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("x2"));
                }
                return sBuilder.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取文件的md5摘要
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <returns>文件摘要</returns>
        public static string AbstractFile(string fileName)
        {
            using (FileStream stream = new FileStream(fileName, FileMode.Open))
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(stream);

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }

                return sb.ToString();
            }
        }

        #endregion

        #region DES加密解密

        public static string DesKey = GlobalContext.SystemConfig.DES_KEY;
        private static byte[] _rgbKey = Encoding.ASCII.GetBytes(DesKey.Substring(0, 8));
        private static byte[] _rgbIV = Encoding.ASCII.GetBytes(DesKey.Insert(0, "w").Substring(0, 8));

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="text">需要加密的值</param>
        /// <returns>加密后的结果</returns>
        public static string DesEncrypt(string text)
        {
            DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
            using (MemoryStream memStream = new MemoryStream())
            {
                CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateEncryptor(_rgbKey, _rgbIV), CryptoStreamMode.Write);
                StreamWriter sWriter = new StreamWriter(crypStream);
                sWriter.Write(text);
                sWriter.Flush();
                crypStream.FlushFinalBlock();
                memStream.Flush();
                return Convert.ToBase64String(memStream.GetBuffer(), 0, (int)memStream.Length);
            }
        }


        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="encryptText">需要解密的值</param>
        /// <returns>解密后的结果</returns>
        public static string DesDecrypt(string encryptText)
        {
            DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
            byte[] buffer = Convert.FromBase64String(encryptText);

            using (MemoryStream memStream = new MemoryStream())
            {
                CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateDecryptor(_rgbKey, _rgbIV), CryptoStreamMode.Write);
                crypStream.Write(buffer, 0, buffer.Length);
                crypStream.FlushFinalBlock();
                memStream.Flush();
                return Encoding.UTF8.GetString(memStream.ToArray());
            }
        }

        #endregion

        #region RSA加密解密

        private readonly static string rsa_encryption_key = GlobalContext.SystemConfig.RSA_ENCRYPTION_KEY;
        private readonly static string rsa_decrypt_key = GlobalContext.SystemConfig.RSA_DECRYPT_KEY;

        /// <summary>
        /// RSA加密(内容+加密key)
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="encryptKey">加密key</param>
        /// <returns>加密后的信息</returns>
        public static string RSAEncrypt(string content)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(rsa_encryption_key);
            UnicodeEncoding ByteConverter = new UnicodeEncoding();
            byte[] DataToEncrypt = ByteConverter.GetBytes(content);
            byte[] resultBytes = rsa.Encrypt(DataToEncrypt, false);
            return Convert.ToBase64String(resultBytes);
        }


        /// <summary>
        /// RSA解密(内容+解密key)
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="descryptKey">解密key</param>
        /// <returns></returns>
        public static string RSADecrypt(string content)
        {
            byte[] dataToDecrypt = Convert.FromBase64String(content);
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            RSA.FromXmlString(rsa_decrypt_key);
            byte[] resultBytes = RSA.Decrypt(dataToDecrypt, false);
            UnicodeEncoding ByteConverter = new UnicodeEncoding();
            return ByteConverter.GetString(resultBytes);
        }

        #endregion
    }
}
